﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Manage_CompanyUpdate : System.Web.UI.Page
{
    private Int64 AccountId;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckCompany);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumComapanyRG);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {


            AccountId = Int64.Parse(SysFun.IsSQL(Request.QueryString["AccountId"]));
            if (!this.IsPostBack)
            {
                HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);
                Grove.ORM.ObjectOperator oo = db.GetObjectOperator();

                HZ.MBSM.Model.Company Company = (HZ.MBSM.Model.Company)oo.Retrieve(typeof(HZ.MBSM.Model.Company), AccountId);
                HZ.MBSM.Model.Area Area = (HZ.MBSM.Model.Area)oo.Retrieve(typeof(HZ.MBSM.Model.Area), Company.AreaId);
                HZ.MBSM.Model.Account Account = (HZ.MBSM.Model.Account)oo.Retrieve(typeof(HZ.MBSM.Model.Account), AccountId);

                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                if (_Account.AccountName == "daiwenjing") Button3.Visible = true;
                else  Button3.Visible = false;
                if (Area != null)
                    this.Label1.Text = "<input  type='text' readonly name='AreaName'  NotNull='所属地区' class='txtbox_100' value='" + Area.AreaName + "' /><input type='text' name='AreaId' style='display:none; width: 64px;' value='" + Company.AreaId + "'>";
                else
                    this.Label1.Text = "<input  type='text' readonly name='AreaName'  NotNull='所属地区' class='txtbox_100' /><input type='text' name='AreaId' style='display:none; width: 64px;' >";

                txtKhyh.Text = SysFun.ToTrim(Company.Khyh);
                txtKhzh.Text = SysFun.ToTrim(Company.Khzh);
                txtKmlxr.Text = SysFun.ToTrim(Company.Kmlxr);
                txtKmlxdh.Text = SysFun.ToTrim(Company.Kmlxdh);

                txtAccountId.Text = SysFun.ToTrim(Company.AccountId);
                txtAccountId.Visible = false;
                //AccountName.Text = Account.AccountName;
                txtUserCode.Text = SysFun.ToTrim(Company.UserCode);

                Address.Text = SysFun.ToTrim(Company.Address);
                RegistAddress.Text = SysFun.ToTrim(Company.RegistAddress);
                CompanyName.Text = SysFun.ToTrim(Company.CompanyName);
                PostCode.Text = SysFun.ToTrim(Company.PostCode);
                Fax.Text = SysFun.ToTrim(Company.Fax);
                Telepone.Text = SysFun.ToTrim(Company.Telepone);
                Lawer.Text = SysFun.ToTrim(Company.Lawer);
                IDcard.Text = SysFun.ToTrim(Company.IDcard);
                Fund.Text = SysFun.ToTrim(Company.Fund.ToString()) == "0" ? "" : SysFun.ToTrim(Company.Fund.ToString());
                LastIncome.Text = SysFun.ToTrim(Company.LastIncome.ToString()) == "0" ? "" : SysFun.ToTrim(Company.LastIncome.ToString());
                if (Company.RegistDate > Convert.ToDateTime("1900-1-1"))
                    RegistDate.Text = Company.RegistDate.ToString();
                RegistDate.Visible = false;
                Email.Text = SysFun.ToTrim(Company.Email);
                switch (Company.Sort)
                {
                    case "0":
                        sort.Text = "生产企业";
                        trgsp.Visible = false;
                        break;
                    case "1":
                        sort.Text = "经营企业";
                        trgsp.Visible = true;
                        break;
                    case "2":
                        sort.Text = "一级代理商";
                        trgsp.Visible = true;
                        break;
                }
                Hsort.Value = Company.Sort;
                //Sort.SelectedValue = SysFun.ToTrim(Company.Sort);
                Introduction.Text = SysFun.ToTrim(Company.Introduction);
               
                txtNetAddress.Text = SysFun.ToTrim(Company.NetAddress);
                txtCodeId.Text = SysFun.ToTrim(Company.CodeId);
                txtFixFund.Text = SysFun.ToTrim(Company.FixFund) == "0" ? "" : SysFun.ToTrim(Company.FixFund);
                txtGspCode.Text = SysFun.ToTrim(Company.GspCode);
                if (Company.GspDate > Convert.ToDateTime("1900-1-1"))
                    txtGspDate.Text = Company.GspDate.ToString();
                if (Company.GspPermitDate > Convert.ToDateTime("1900-1-1"))
                    txtGspPermitDate.Text = Company.GspPermitDate.ToString();
                txtGspRange.Text = SysFun.ToTrim(Company.GspRange);
                if (Company.LCDate > Convert.ToDateTime("1900-1-1"))
                    txtLCDate.Text = Company.LCDate.ToString();



                txtsos.Text = SysFun.ToTrim(Company.SOS);

                txtLCPermission.Text = SysFun.ToTrim(Company.LCPermission);
                txtPermission.Text = SysFun.ToTrim(Company.Permission);
               
                if (Company.IsGsp == "1")
                {
                    chkIsGsp.Checked = true;
                }
                else
                {
                    chkIsGsp.Checked = false;
                }
                if (Company.IsLC == "1")
                {
                    chkIsLC.Checked = true;
                }
                else
                {
                    chkIsLC.Checked = false;
                }
                if (Company.IsLX == 1)
                {
                    islx.Visible = true;
                    txtRegistermeneymark.Text = Company.Registermeneymark;

                    txtPS.Text = Company.hospitalPSnumber;

                    txtcompanyParDuty.Text = Company.companyParDuty;

                    txtStorageArea.Text = Company.StorageArea;


                    if (Company.breachoflaw == "0" || Company.breachoflaw.Trim() == "")
                    { RadioButtonList2.SelectedIndex = 1; }
                    else
                    { RadioButtonList2.SelectedIndex = 0; }
                }
                else

                {
                    islx.Visible = false;
                }
                rdoLcLevel.SelectedValue = Company.LcLevel;
                txtTotalFund.Text = SysFun.ToTrim(Company.TotalFund) == "0" ? "" : SysFun.ToTrim(Company.TotalFund);
                txtBusinessScope.Text = SysFun.ToTrim(Company.BusinessScope);
                txtMemo1.Text = SysFun.ToTrim(Company.memo1);
                txtWarrant.Text = SysFun.ToTrim(Company.Warrant);           

                if (SysFun.ToTrim(Company.CodeDate) == "1900-1-1 0:00:00")
                {
                    txtCodeDate.Text = "";
                }
                else
                {
                    int ndate = SysFun.DoDateDiff(SysFun.DateInterval.Day, System.DateTime.Now, Convert.ToDateTime(Company.CodeDate));
                    if (ndate <= 0)
                    {
                        LabelYYZZ.Text = "过期";
                        LabelYYZZ.Visible = true;
                    }
                    else if (ndate <= 60)
                    {
                        LabelYYZZ.Text = "距离过期时间还有 " + ndate + " 天";
                        LabelYYZZ.Visible = true;
                    }
                    txtCodeDate.Text = SysFun.ToTrim(Company.CodeDate);

                }

                
                if (SysFun.ToTrim(Company.PermissionDate) == "1900-1-1 0:00:00")
                {
                    txtPermissionDate.Text = "";
                }
                else
                {
                    int ndate = SysFun.DoDateDiff(SysFun.DateInterval.Day, System.DateTime.Now, Convert.ToDateTime(Company.PermissionDate));
                    if (ndate <= 0)
                    {
                        LabelXpermis.Text = "过期";
                        LabelXpermis.Visible = true;
                    }
                    else if (ndate <= 60)
                    {
                        LabelXpermis.Text = "距离过期时间还有 " + ndate + " 天";
                        LabelXpermis.Visible = true;
                    }
                    txtPermissionDate.Text = SysFun.ToTrim(Company.PermissionDate);

                }



                if (SysFun.ToTrim(Company.WarrantDate) == "1900-1-1 0:00:00")
                {
                    txtWarrantDate.Text = "";
                }
                else
                { 
                       int ndate=  SysFun.DoDateDiff(SysFun.DateInterval.Day, System.DateTime.Now, Convert.ToDateTime(Company.WarrantDate));
                        if(ndate<=0)
                        {
                          LabelXuKeZheng.Text = "过期";
                          LabelXuKeZheng.Visible = true;
                        }
                        else if (ndate <= 60)
                        {
                            LabelXuKeZheng.Text = "距离过期时间还有 " + ndate + " 天";
                            LabelXuKeZheng.Visible = true;
                        } 
                         txtWarrantDate.Text = SysFun.ToTrim(Company.WarrantDate);
                         
                }
                if (SysFun.ToTrim(Company.GspDate) == "1900-1-1 0:00:00")
                {
                    txtGspDate.Text = "";
                }
                else
                { 
                    int ndate = SysFun.DoDateDiff(SysFun.DateInterval.Day, System.DateTime.Now, Convert.ToDateTime(Company.GspDate));
                    if (ndate <= 0)
                    {
                        LabelGSP.Text = "过期";
                        LabelGSP.Visible = true;
                    }
                    else if (ndate <= 60)
                    {
                        LabelGSP.Text = "距离过期时间还有 " + ndate + " 天";
                        LabelGSP.Visible = true;
                    } 

                    txtGspDate.Text = SysFun.ToTrim(Company.GspDate);
                }
                txtMemo5.Text = SysFun.ToTrim(Company.memo5);
                this.mobilephone.Text = SysFun.ToTrim(Company.memo10);

                txtMemo2.Text = SysFun.ToTrim(Company.memo2);
                //if (SysFun.ToTrim(Company.memo2) == "1" || SysFun.ToTrim(Company.memo2) == "2")
                //{
                //    pnl.Enabled= true;
                //}
                //else
                //{
                //    pnl.Enabled = false;
                //}

                txtMemo2.Visible = false;
                oo.Dispose();
                db.Close();
                oo = null;
                db = null;
            }
        }

    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        Int64 AccountId_SH;
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            AccountId_SH = SysFun.ToLong(_Account.AccountId);
            int rowCun = 0;
            Db db1 = new Db();
            System.Data.IDataReader dr;
            //string strSql = "select count(accountid) as cun from dbo.v_account where ( dwname= '" + SysFun.ToTrim(CompanyName.Text) + "' or usercode='" + SysFun.ToTrim(txtUserCode.Text) + "') AND accountid<>" + AccountId.ToString();
            string strSql = "select count(accountid) as cun from dbo.v_account where usercode='" + SysFun.ToTrim(txtUserCode.Text) + "' AND accountid<>" + AccountId.ToString();
          
            dr = db1.DataReader(strSql);
            if (dr.Read())
            {
                rowCun = SysFun.ToInt(dr["cun"]);
            }
            dr.Close();
            dr.Dispose();
            db1.Close();
            if (rowCun > 0)
            {
                Response.Write("<script language=JavaScript>alert('该用户在数据库中已经存在！！！');</script>");
            }
            else
            {
                HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);
                Grove.ORM.ObjectOperator oo = db.GetObjectOperator();

                HZ.MBSM.Model.Company Company = (HZ.MBSM.Model.Company)oo.Retrieve(typeof(HZ.MBSM.Model.Company), AccountId);
                if (Company != null)
                {
                    Company.Registermeneymark = txtRegistermeneymark.Text.Trim();
                    Company.hospitalPSnumber = txtPS.Text.Trim();
                    Company.companyParDuty = txtcompanyParDuty.Text.Trim();
                    Company.StorageArea = txtStorageArea.Text.Trim();
                    Company.breachoflaw = RadioButtonList2.SelectedValue;

                    Company.Khyh = SysFun.ToTrim(txtKhyh.Text);
                    Company.Khzh = SysFun.ToTrim(txtKhzh.Text);
                    Company.Kmlxr = SysFun.ToTrim(txtKmlxr.Text);
                    Company.Kmlxdh = SysFun.ToTrim(txtKmlxdh.Text);

                    Company.AccountId = AccountId;
                    Company.Address = Address.Text;
                    Company.RegistAddress = RegistAddress.Text;
                    Company.AreaId = SysFun.ToInt(Request.Form["AreaId"]);
                    Company.UserCode = txtUserCode.Text.ToString();
                    Company.CompanyName = CompanyName.Text;
                    Company.PostCode = SysFun.ToTrim(PostCode.Text);
                    Company.Fax = Fax.Text;
                    Company.Telepone = Telepone.Text;
                    Company.Lawer = Lawer.Text;
                    Company.IDcard = IDcard.Text;
                    Company.Fund = double.Parse(SysFun.ToDec(Fund.Text).ToString());
                    Company.LastIncome = double.Parse(SysFun.ToDec(LastIncome.Text).ToString());
                    if (SysFun.ToTrim(RegistDate.Text.ToString()) != "")
                        Company.RegistDate = Convert.ToDateTime(RegistDate.Text);
                    else
                        Company.RegistDate = System.DateTime.Now;
                    Company.Email = Email.Text;
                    Company.Introduction = Introduction.Text;
                    Company.NetAddress = SysFun.ToTrim(txtNetAddress.Text);
                    Company.CodeId = SysFun.ToTrim(txtCodeId.Text);
                    if (SysFun.ToTrim(txtCodeDate.Text) != "")
                    {
                        Company.CodeDate = Convert.ToDateTime(txtCodeDate.Text);
                    }
                    else
                    {
                        Company.CodeDate = Convert.ToDateTime("1900-1-1");
                    }
                    Company.Permission = SysFun.ToTrim(txtPermission.Text);
                    Company.PermissionDate = SysFun.ToDateTime(txtPermissionDate.Text);
                    Company.IsLC = SysFun.ToTrim(SysFun.ToInt(chkIsLC.Checked));
                    Company.LcLevel = SysFun.ToTrim(rdoLcLevel.SelectedValue);
                    Company.LCPermission = SysFun.ToTrim(txtLCPermission.Text);
                    if (SysFun.ToTrim(txtLCDate.Text) != "")
                    {
                        Company.LCDate = Convert.ToDateTime(txtLCDate.Text);
                    }
                    else
                    {
                        Company.LCDate = Convert.ToDateTime("1900-1-1");
                    }
                    Company.IsGsp = SysFun.ToTrim(SysFun.ToInt(chkIsGsp.Checked));
                    Company.GspCode = SysFun.ToTrim(txtGspCode.Text);
                    if (SysFun.ToTrim(txtGspDate.Text) != "")
                    {
                        Company.GspDate = Convert.ToDateTime(txtGspDate.Text);
                    }
                    else
                    {
                        Company.GspDate = Convert.ToDateTime("1900-1-1");
                    }
                    Company.GspRange = SysFun.ToTrim(txtGspRange.Text);
                    if (SysFun.ToTrim(txtGspPermitDate.Text) != "")
                    {
                        Company.GspPermitDate = Convert.ToDateTime(txtGspPermitDate.Text);
                    }
                    else
                    {
                        Company.GspPermitDate = Convert.ToDateTime("1900-1-1");
                    }
                    Company.Warrant = SysFun.ToTrim(txtWarrant.Text);
                    if (SysFun.ToTrim(txtWarrantDate.Text) != "")
                    {
                        Company.WarrantDate = Convert.ToDateTime(txtWarrantDate.Text);
                    }
                    else
                    {
                        Company.WarrantDate = Convert.ToDateTime("1900-1-1");
                    }
                    Company.BusinessScope = SysFun.ToTrim(txtBusinessScope.Text);
                    Company.memo1 = SysFun.ToTrim(txtMemo1.Text);
                    Company.TotalFund = SysFun.ToDouble(txtTotalFund.Text);
                    Company.FixFund = SysFun.ToDouble(txtFixFund.Text);
                    Company.AccountId_SH = AccountId_SH;
                    Company.CheckStatus = "1";
                    Company.memo5 = SysFun.ToTrim(txtMemo5.Text);
                    Company.memo2 = SysFun.ToTrim(txtMemo2.Text);
                    Company.memo6 = "";
                    Company.memo10 = SysFun.ToTrim(mobilephone.Text);
                    Company.CheckTime = System.DateTime.Now;
                    Company.SOS = SysFun.ToTrim(txtsos.Text);
                    oo.Update(Company);

                }
                oo.Dispose();
                db.Close();
                oo = null;
                db = null;
                //HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "account", strSql, "../Magage/CompanyUpdate.aspx");

                Response.Write("<script>alert('保存成功！！！');</script>");
                Response.Write("<script>window.opener.location.reload();window.close();</script>");
            }
        }
        else
        {
            Response.Write("<script language=JavaScript>alert('您无权审核企业信息！！！');</script>");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string str_sele = "";
        string strmem6 = "";
        if (Checkfaren.Checked == true)
        {
            strmem6 += " 生产许可证不正确！\n";
        }
        if (CheckXuKe.Checked == true)
        {
            strmem6 += "企业法人不正确！\n";
        }
        if (CheckZhiZhao.Checked == true)
        {
            strmem6 += "营业执照不正确！ \n";
        }
        if (CheckBoxGMP.Checked == true)
        {
            strmem6 += "GMP证书有问题！\n";
        }
        if (CheckBoxGSP.Checked == true)
        {
            strmem6 += "GSP证书有问题！\n";
        }
        if (ChkWNS.Checked == true)
        {
            strmem6 += "营业执照未年审！\n";
        }
        if (strmem6 == "" && !CheckBoxother.Checked)
        {
            Response.Write("<script>alert('审核未通过的原因不能为空！！！');</script>");
        }
        else
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            str_sele = "update company set memo6='" + strmem6 + "',CheckStatus=2,AccountId_SH='" + _Account.AccountId + "',checktime=getdate() where AccountId ='" + AccountId + "'";
            Db dd = new Db();
            dd.Command(str_sele);
            dd.Close();
            dd = null;
            Response.Redirect("CompanyNoPass.aspx?AccountId=" + AccountId.ToString());
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Int64 AccountId_SH, AccountId;
        string strSql;
        AccountId = Int64.Parse(SysFun.IsSQL(Request.QueryString["AccountId"]));
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            AccountId_SH = SysFun.ToLong(_Account.AccountId);
            Db db = new Db();
            strSql = "update Company set  memo6 = null ,CheckStatus=3,AccountId_SH='" + AccountId_SH + "',checktime=getdate() where AccountId=" + AccountId.ToString();
            db.Command(strSql);
            db.Close();
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "Company", strSql, "../Magage/CompanyUpdate.aspx");
            db = null;
            Response.Write("<script>window.opener.location.reload();window.close();</script>");
        }
        else
        {
            Response.Write("<script language=JavaScript>alert('您无权审核企业信息！！！');</script>");
        }

    }
    //保存并生成随机密码
    protected void Button3_Click(object sender, EventArgs e)
    {
         HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
         string pwd = HZ.MBSM.BLL.Company.GetUercodePWD(AccountId.ToString(), _Account.AccountId.ToString(), Hsort.Value);
         if (pwd.Trim() != "")
         {
             Labelpd.Text ="密码："+ pwd.Trim();
             Response.Write("<script language=JavaScript>alert('生成成功！');</script>");
         }
         else
             Response.Write("<script language=JavaScript>alert('生成失败，请重新点击！');</script>");
    }
}
